package bio;

/*
 * Author: JacksonCoder
 * FileName: SocketClient
 * Version: 1.0.0
 * Date: 2025/5/6
 */

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class SocketClient {
    public static void main(String[] args) {
        try (Socket socket = new Socket("127.0.0.1", 8080);
             BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
             BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
             Scanner scanner = new Scanner(System.in)) {

            System.out.println("已连接到服务器，输入消息 (输入 bye 退出)：");

            while (true) {
                System.out.print("你：");
                String userInput = scanner.nextLine();

                writer.write(userInput);
                writer.newLine();
                writer.flush();

                String serverResponse = reader.readLine();
                System.out.println("服务器：" + serverResponse);

                if ("bye".equalsIgnoreCase(userInput.trim())) {
                    System.out.println("连接已关闭。");
                    break;
                }
            }

        } catch (IOException e) {
            System.out.println("连接异常：" + e.getMessage());
        }
    }
}
